// pages/login/index.js
let App = getApp();
Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        
    },
    onLoad: function() {
        var that = this;
        // 查看是否授权
        wx.getSetting({
            success: function(res) {
                if (res.authSetting['scope.userInfo']) {
                    wx.getUserInfo({
                        success: function(res) {
                            //从数据库获取用户信息
                            that.queryUsreInfo();
                            console.log('1111')
                            //用户已经授权过
                            wx.reLaunch({
                                url: "/pages/phone_login/phone_login",
                            })
                        }
                    });
                }
            }
        })
    },
    /**
     * 组件的方法列表
     */
    methods: {
        authorLogin: function(e) {
            console.log(e)
            let _this = this;
            if (e.detail.errMsg !== 'getUserInfo:ok') {
                //用户按了拒绝按钮
                wx.showModal({
                    title: '警告',
                    content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
                    showCancel: false,
                    confirmText: '返回授权',
                    success: function(res) {
                        if (res.confirm) {
                            console.log('用户点击了“返回授权”')
                        }
                    }
                })
            }
            wx.showLoading({
                title: "正在登录",
                mask: true
            });
            // 执行微信登录
            wx.login({
                success: function(res) {
                    // 发送用户信息
                    App._post_form('coach/index/wxLogin', {
                        code: res.code,
                    }, function(result) {
                        // 记录token user_id
                        console.log(result)
                        wx.setStorageSync('openid', result.list.openid);
                        App._get('coach/index/getUserinfo', {
                            openid: result.list.openid,
                            nickname: e.detail.userInfo.nickName,
                            gender: e.detail.userInfo.gender,
                            avatarUrl: e.detail.userInfo.avatarUrl,
                            encryptedData: e.detail.encryptedData,
                            iv: e.detail.iv,
                            city: e.detail.userInfo.city
                        }, function(result) {
                            console.log(result)
                        }, false, function() {

                        })
                        // 跳转到登录界面
                        wx.reLaunch({
                            url: "/pages/phone_login/phone_login"
                        });
                    }, false, function() {
                        wx.hideLoading();
                    });

                }
            });
        },

        /**
         * 授权成功 跳转回原页面
         */
        navigateBack: function() {

        },
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})